﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

public partial class Admin_Contacts : System.Web.UI.Page
{
    protected int _pageId;
    protected LocalFunctions FUNC;
    int iLangID;
    protected string sPageTitle;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin_AdminMasterPage)Master).FUNC;
        GetParameters();
        if (!IsPostBack)
        {
            imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + Session["Image"];
            Session["Image"] = string.Empty;
        }
        else
        {
            imgPreview.Src = FUNC.FullAppPath + "/Gallery/" + Session["image"].ToString();
        }
        iLangID = (Session["Language"] != null) ? Convert.ToInt32(Session["Language"]) : 1;
        if(!IsPostBack)
            ddlLanguageChoose.SelectedValue = iLangID.ToString() ;

        this.Page.ClientScript.RegisterOnSubmitStatement(
           this.GetType(),
           "AjaxHack",
           "for ( var i = 0; i < parent.frames.length; ++i ) if (parent.frames[i].FCK) parent.frames[i].FCK.UpdateLinkedField();"
           );
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        bool changeLang = false;
        //Get specific page data
        if (iLangID != Convert.ToInt32(Session["Language"]))
        {
            Session["Language"] = iLangID;
            changeLang = true;
        }
        if (!IsPostBack || changeLang)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(6);
            Dal.arParams.Add(iLangID);
            DataTable DT = Dal.ExecuteDBsp("RD_SelectStaticPageById", Dal.arParams);
            if (DT != null)
                if (DT.Rows.Count > 0)
                {
                    hfID.Value = DT.Rows[0]["ID"].ToString();
                    sPageTitle = DT.Rows[0]["PageTitle"].ToString();
                    fckPageContent.Value = DT.Rows[0]["PageContent"].ToString();
                    imgPreview.Src=FUNC.FullAppPath + "/Gallery/"+DT.Rows[0]["imageLink"].ToString();
                    Session["Image"] = DT.Rows[0]["imageLink"].ToString();
                }
            txtEmails.Text= Dal.ExecuteDBspOneValue("Admin_GetEmail",null).ToString();
        }
    }

    private void GetParameters()
    {
        _pageId = Convert.ToInt32(Request.QueryString["Id"]);
        _pageId=(_pageId == 0) ? 1 : _pageId;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Int32 iID = Convert.ToInt32(hfID.Value);

            Dal.arParams.Clear();
            Dal.arParams.Add(iID);
            Dal.arParams.Add(txtEmails.Text.Trim());
            Dal.arParams.Add(fckPageContent.Value);
            Dal.arParams.Add(Session["Image"]);
            Dal.ExecuteDBsp("Admin_UpdateStaticPageById", Dal.arParams, true);
        
            Dal.arParams.Clear();
            Dal.arParams.Add(txtEmails.Text);
            Dal.ExecuteDBsp("Admin_SetEmail", Dal.arParams, true);
        }
    }
    
    protected void ddlLanguageChoose_SelectedIndexChanged(object sender, EventArgs e)
    {
        iLangID = Convert.ToInt32(((DropDownList)sender).SelectedValue);
       
    }

    protected void uplImage_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
        string script = string.Empty;
        string[] sExtension = e.filename.Split('.');
        string sFileName = "ttl_"+Helper.GenerateUniqueFileName() + "." + sExtension[sExtension.Length - 1];
        string sPath = Server.MapPath("~/Gallery/") +sFileName;
        uplImage.SaveAs(sPath);

        Session["Image"] = sFileName;
        ScriptManager.RegisterClientScriptBlock(uplImage, uplImage.GetType(), "img",
"top.document.getElementById('ctl00_ContentPlaceHolder1_imgPreview').src='" + FUNC.FullAppPath + "/Gallery/" + sFileName + "';",
true);
    }
}
